package com.ddtech.swagger2;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;
/**
 * @author bin
 * @date 2023-05-30
 */
@Configuration
@EnableSwagger2
public class Knife4jConfig {
    @Bean
    public Docket docket(Environment environment) {
        //设置只在开发中环境中启动swagger
        Profiles profiles = Profiles.of("dev");
        //表示如果现在是dev环境，则返回true 开启swagger
        boolean flag = environment.acceptsProfiles(profiles);
        /*添加接口请求头参数配置 没有的话 可以忽略*/
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("Authorization")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //是否启动swagger 默认启动
                .enable(flag)
                .select()
                .build()

                .globalOperationParameters(pars);
    }

    private ApiInfo apiInfo() {
        //作者信息
        Contact author = new Contact("技能培训管理平台", "http://110.41.16.105:9000/doc.html", "865373452@qq.com");
        return new ApiInfo(
                "技能培训管理平台",
                "技能培训管理平台Api接口文档",
                "1.0",
                "urn:tos",
                author,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );

    }
}